package com.yinp.proapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.navigation.NavigationBarView;
import com.yinp.proapp.R;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.constant.Constant;
import com.yinp.proapp.databinding.ActivityMajorBinding;
import com.yinp.proapp.fragment.bottompage.HomeFragment;
import com.yinp.proapp.fragment.bottompage.MeFragment;
import com.yinp.proapp.fragment.bottompage.RecreationFragment;
import com.yinp.proapp.fragment.bottompage.StudyFragment;
import com.yinp.proapp.fragment.bottompage.ToolsFragment;
import com.yinp.proapp.module.task.activity.AddLabelActivity;
import com.yinp.proapp.module.task.activity.AddUndeterminedActivity;
import com.yinp.proapp.utils.JPushUtils;
import com.yinp.proapp.utils.StatusBarUtil;

/**
 * 首页
 */
public class MajorActivity extends AppBaseActivity<ActivityMajorBinding> {
    private Fragment curFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    private HomeFragment homeFragment;
    private StudyFragment studyFragment;
    private ToolsFragment toolsFragment;
    private RecreationFragment recreationFragment;
    private MeFragment meFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        fragmentManager = getSupportFragmentManager();
        if (savedInstanceState != null) {
            homeFragment = (HomeFragment) fragmentManager.findFragmentByTag("homeFragment");
            studyFragment = (StudyFragment) fragmentManager.findFragmentByTag("studyFragment");
            toolsFragment = (ToolsFragment) fragmentManager.findFragmentByTag("toolsFragment");
            recreationFragment = (RecreationFragment) fragmentManager.findFragmentByTag("recreationFragment");
            meFragment = (MeFragment) fragmentManager.findFragmentByTag("meFragment");
        }
        super.onCreate(savedInstanceState);
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        initClick(bd.header.ivImg);
        bd.header.ivImg.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initViews() {
        bd.header.headerBackImg.setVisibility(View.GONE);
        bd.header.headerCenterTitle.setText("首页");
        JPushUtils.setAlias(getApplicationContext());
        //这个id如何改呢
        bd.bottomNavigationView.setOnItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.one:
                    bd.header.headerCenterTitle.setText("首页");
                    chooseFragment(0);
                    break;
                case R.id.two:
                    bd.header.headerCenterTitle.setText("学习");
                    chooseFragment(1);
                    break;
                case R.id.three:
                    bd.header.headerCenterTitle.setText("工具");
                    chooseFragment(2);
                    break;
                case R.id.four:
                    bd.header.headerCenterTitle.setText("娱乐");
                    chooseFragment(3);
                    break;
                case R.id.five:
                    bd.header.headerCenterTitle.setText("我的");
                    chooseFragment(4);
                    break;
            }
            return true;
        });

        bd.bottomNavigationView.setItemIconTintList(null);
        bd.bottomNavigationView.setItemTextColor(getColorStateList(R.color.selector_8a8a8a_ff4d4d));
        bd.bottomNavigationView.setItemIconSize((int) getResources().getDimension(R.dimen._28dp));
        bd.bottomNavigationView.setLabelVisibilityMode(NavigationBarView.LABEL_VISIBILITY_LABELED);

        chooseFragment(0);
    }

    private void chooseFragment(int position) {
        fragmentTransaction = fragmentManager.beginTransaction();
        switch (position) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.fl_content, homeFragment, "homeFragment");
                }
                if (curFragment != null) {
                    fragmentTransaction.hide(curFragment);
                }
                bd.header.ivImg.setVisibility(View.VISIBLE);
                curFragment = homeFragment;
                fragmentTransaction.show(curFragment);
                fragmentTransaction.commitNow();
                break;
            case 1:
                if (studyFragment == null) {
                    studyFragment = new StudyFragment();
                    fragmentTransaction.add(R.id.fl_content, studyFragment, "studyFragment");
                }
                if (curFragment != null) {
                    fragmentTransaction.hide(curFragment);
                }
                bd.header.ivImg.setVisibility(View.GONE);
                curFragment = studyFragment;
                fragmentTransaction.show(curFragment);
                fragmentTransaction.commitNow();
                break;
            case 2:
                if (toolsFragment == null) {
                    toolsFragment = new ToolsFragment();
                    fragmentTransaction.add(R.id.fl_content, toolsFragment, "toolsFragment");
                }
                if (curFragment != null) {
                    fragmentTransaction.hide(curFragment);
                }
                bd.header.ivImg.setVisibility(View.GONE);
                curFragment = toolsFragment;
                fragmentTransaction.show(curFragment);
                fragmentTransaction.commitNow();
                break;
            case 3:
                if (recreationFragment == null) {
                    recreationFragment = new RecreationFragment();
                    fragmentTransaction.add(R.id.fl_content, recreationFragment, "recreationFragment");
                }
                if (curFragment != null) {
                    fragmentTransaction.hide(curFragment);
                }
                bd.header.ivImg.setVisibility(View.GONE);
                curFragment = recreationFragment;
                fragmentTransaction.show(curFragment);
                fragmentTransaction.commitNow();
                break;
            case 4:
                if (meFragment == null) {
                    meFragment = new MeFragment();
                    fragmentTransaction.add(R.id.fl_content, meFragment, "meFragment");
                }
                if (curFragment != null) {
                    fragmentTransaction.hide(curFragment);
                }
                bd.header.ivImg.setVisibility(View.GONE);
                curFragment = meFragment;
                fragmentTransaction.show(curFragment);
                fragmentTransaction.commitNow();
                break;
        }
    }

    /**
     * 直接退回到桌面,不会onCreate,两种方案
     */
//    @Override
//    public void onBackPressed() {
//        Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
//        launcherIntent.addCategory(Intent.CATEGORY_HOME);
//        startActivity(launcherIntent);
//    }
    @Override
    public void onBackPressed() {
        moveTaskToBack(false);
    }
    //    private static Boolean isQuit = false;
//    Timer timer = new Timer();
//
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            if (!isQuit) {
//                isQuit = true;
//                Toast.makeText(getBaseContext(), getString(R.string.exit_tips),
//                        Toast.LENGTH_SHORT).show();
//                timer.schedule(new TimerTask() {
//                    @Override
//                    public void run() {
//                        isQuit = false;
//                    }
//                }, 1500);
//            } else {
//                finish();
//            }
//        }
//        return false;
//    }

    @Override
    public void onClick(View v) {
        if (v == bd.header.ivImg) {
            if (homeFragment.bd.materialViewPager.getCurrentItem() == 0) {
                goToActivity(AddUndeterminedActivity.class, Constant.REQUEST_CODE);
            } else if (homeFragment.bd.materialViewPager.getCurrentItem() == 1) {
                goToActivity(AddLabelActivity.class, Constant.REQUEST_CODE2);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constant.RESULT_CODE) {
            if (requestCode == Constant.REQUEST_CODE) {
                if (homeFragment.todayUndeterminedFragment.toDoCurFragment != null) {
                    homeFragment.todayUndeterminedFragment.toDoCurFragment.initDataList();
                }
            } else if (requestCode == Constant.REQUEST_CODE2) {
                if (homeFragment.labelFragment != null) {

                }
            }
        }
    }

    /**
     * APP崩溃，导致一级页面的fargmentUI层叠
     *
     * @param outState
     */
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
//        super.onSaveInstanceState(outState);
    }
}